package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.ehs.dto.check.CheckGroupDto;
import qc.module.ehs.dto.check.CheckGroupFullDto;
import qc.module.ehs.entity.EhsCheckGroup;

import java.util.List;

/**
 * EhsCheckGroupMapper
 *
 * @author QuCheng Tech
 * @create 2024/10/23
 */
@Mapper
public interface EhsCheckGroupMapper {
    EhsCheckGroupMapper MAPPER = Mappers.getMapper(EhsCheckGroupMapper.class);

    /**
     * entity -> dto
     * @param en
     * @return
     */
    @Mapping(target = "status", source = "status.index")
    CheckGroupDto toDto(EhsCheckGroup en);

    /**
     * List<entity> -> List<dto>
     * @param ens
     * @return
     */
    List<CheckGroupDto> toDtoList(List<EhsCheckGroup> ens);

    /**
     * dto -> entity
     * @param dto
     * @return
     */
    @Mapping(target = "status", ignore = true)
    EhsCheckGroup toEntity(CheckGroupDto dto);

    default EhsCheckGroup dtoToEntity(CheckGroupDto dto) {
        EhsCheckGroup en = EhsCheckGroupMapper.MAPPER.toEntity(dto);

        en.setStatus(getResourceStatusFlagEnum(dto.getStatus()));

        return en;
    }

    @Mapping(target = "status", source = "status.index")
    @Mapping(target = "items", ignore = true)
    CheckGroupFullDto  toCheckGroupFullDto(EhsCheckGroup en);

    /**
     * List<entity> -> List<CheckGroupFullDto>
     * @param ens
     * @return
     */
    @Mapping(target = "items", ignore = true)
    List<CheckGroupFullDto> toCheckGroupFullDtoList(List<EhsCheckGroup> ens);

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认状态为0--正常
        return ResourceStatusFlagEnum.NORMAL;
    }
}
